/**
 * Copyright 2011 ArcBees Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package vn.nichecafe.client.presenter;

import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.RequestTabsHandler;
import com.gwtplatform.mvp.client.TabView;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.RequestTabs;
import com.gwtplatform.mvp.client.annotations.TabInfo;
import com.gwtplatform.mvp.client.proxy.NonLeafTabContentProxy;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import vn.nichecafe.client.NameTokens;
import vn.nichecafe.client.gin.ClientGinjector;

/**
 * A sample {@link com.gwtplatform.mvp.client.TabContainerPresenter
 * TabContainerPresenter} appearing as a tab within {@link MainPagePresenter}
 * and itself containing two tabs. When the tab for {@link AboutPresenter} is
 * clicked, them {@link AboutNicheBandPresenter} is displayed.
 * <p />
 * It demonstrates the option 2 described in {@link TabInfo}, together with the
 * use of the {@code nameToken} parameter of {@code @TabInfo} to specify which
 * place to show when the tab is clicked.
 * 
 * @author Christian Goudreau
 * @author Philippe Beaudoin
 */
public class AboutPresenter extends
		AboutPresenterBase<AboutPresenter.MyView, AboutPresenter.MyProxy> {
	/**
	 * {@link AboutPresenter}'s proxy.
	 */
	@ProxyCodeSplit
	public interface MyProxy extends NonLeafTabContentProxy<AboutPresenter> {
	}

	@TabInfo(container = MainPagePresenter.class, priority = 1, nameToken = NameTokens.aboutNicheBandPage)
	static String getTabLabel(ClientGinjector ginjector) {
		return ginjector.getMyConstants().about();
	}

	/**
	 * {@link AboutPresenter}'s view.
	 */
	public interface MyView extends TabView {
	}

	/**
	 * This will be the event sent to our "unknown" child presenters, in order
	 * for them to register their tabs.
	 */
	@RequestTabs
	public static final Type<RequestTabsHandler> TYPE_RequestTabs = new Type<RequestTabsHandler>();
	private final PlaceManager placeManager;

	@Inject
	public AboutPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, PlaceManager placeManager) {
		super(eventBus, view, proxy, TYPE_RequestTabs);
		this.placeManager = placeManager;
	}

	@Override
	public void onReset() {
		super.onReset();
		MyProxy proxy = getProxy();
		proxy.changeTab(proxy.getTabData(), placeManager
				.getCurrentPlaceRequest().getNameToken());
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPagePresenter.TYPE_SetTabContent,
				this);
	}
}